package com.hao.study.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hao.study.entity.User;
import com.hao.study.mapper.UserMapper;
import com.hao.study.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author: author
 * @description:
 * @date: 2024/6/5 14:44
 * @version: 1.0
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public PageInfo<User> getUsersList(Integer pageNum, Integer pageSize,String keyword) {
        PageHelper.startPage(pageNum, pageSize);
        //条件构造器
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//
        if (StringUtils.isNotBlank(keyword)){
            queryWrapper.like("username", keyword);
        }
        queryWrapper.orderBy(true,false,"create_time");

        List<User> users = userMapper.selectList(queryWrapper);
        return PageInfo.of(users);
    }

    @Override
    public User getAdminUser(User user) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",user.getUsername());
        queryWrapper.eq("password",user.getPassword());
        //1为普通用户 权限大于 1     为管理员
        queryWrapper.gt("role","1");
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public User getNormalUser(User user) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",user.getUsername());
        queryWrapper.eq("password",user.getPassword());
        queryWrapper.eq("role","1");
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public void createUser(User user) {
        if (user == null) {
            throw new IllegalArgumentException("User object is null");
        }
        userMapper.insert(user);
    }

    @Override
    public void updateUser(User user) {
        if (user.getId() == null|| Objects.equals(user.getUsername(), "")){
            user.setId(this.getUserByUserName(user.getUsername()).getId());
        }
        userMapper.updateById(user);
    }

    @Override
    public void deleteUser(Integer id) {
        System.out.println("id======="+id);
        userMapper.deleteById(id);
    }

    @Override
    public User getUserByToken(String token) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("token",token);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public User getUserDetail(String userId) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",userId);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public User getUserByUserName(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", username);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public PageInfo<User> selcetPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> users = this.selcetAll();
        return PageInfo.of(users);
    }

    @Override
    public List<User> selcetAll() {
        return userMapper.selectList(new QueryWrapper<>());
    }


}
